package com.njit.campuslifeboot.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.njit.campuslifeboot.annotation.AuthAnnotation;
import com.njit.campuslifeboot.entity.dto.AddCommentDTO;
import com.njit.campuslifeboot.entity.dto.CommentItem;
import com.njit.campuslifeboot.entity.po.User;
import com.njit.campuslifeboot.service.ILifeInfoCommentService;
import com.njit.campuslifeboot.service.IUserService;
import com.njit.campuslifeboot.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Axin
 * @since 2022-11-08
 */
@RestController
@RequestMapping("/life-info-comment")
public class LifeInfoCommentController {
    @Autowired
    private ILifeInfoCommentService lifeInfoCommentService;
    @Autowired
    private IUserService userService;

    @PostMapping("/addComment")
    @AuthAnnotation
    public JsonResult<Void> addComment(@RequestBody AddCommentDTO addCommentDTO, HttpServletRequest request) {
        String username = request.getHeader("auth");
        User user = userService.getOne(new QueryWrapper<User>().eq("USERNAME", username));
        boolean insert = lifeInfoCommentService.addComment(addCommentDTO, user.getId());
        return insert ? JsonResult.success() : JsonResult.fail("addComment error");
    }

    @GetMapping("/getComment")
    public JsonResult<List<CommentItem>> getComment(@RequestParam("infoId") Integer id) {
        List<CommentItem> comments = lifeInfoCommentService.getComments(id);
        return JsonResult.success(comments);
    }
}

